/*
 * @Author: Songjq
 * @Date: 2021-10-28 10:11:03
 * @Desscription: 组织树节点公共组件
 */
import {
  getBaseOrgTreeList, // 基准组织树懒加载
  getBusinessOrgTreeList // 业务组织树懒加载
} from "@http/system/dept";
export default {
  props: {
    requestUrlType: {
      // 请求的接口类型 基准组织/业务组织
      type: String,
      default: "isBaseOrgTree"
    },
    sourceAppType: {
      // 基准组织类型
      type: String,
      default: ""
    }
  },
  data() {
    return {
      loading: false,
      deptDatas: [],
      defaultExpandedKeys: [], // 默认展开的树节点
      defaultProps: {
        label: "name",
        children: "children",
        isLeaf: "leaf"
      }
    };
  },

  methods: {
    // 通过关键字检索树节点
    getNodeSearch(keyword, tagId) {
      this.deptDatas = [];
      this.loading = true;
      this.defaultExpandedKeys = [];
      let typeUrlFn = "";
      switch (this.requestUrlType) {
        // 基准组织
        case "isBaseOrgTree":
          typeUrlFn = getBaseOrgTreeList({
            pid: null,
            keyword: keyword,
            sourceAppId: this.sourceAppType
          });
          break;

        case "isBusinessOrgTree":
          // 业务组织树
          typeUrlFn = getBusinessOrgTreeList({
            pid: null,
            keyword: keyword,
            tagId: tagId
          });
          break;
      }

      typeUrlFn.then(res => {
        this.loading = false;
        if (this.isChecked) {
          this.$nextTick(() => {
            this.$refs.deptTree.setCheckedNodes(this.defaultCheckedKeys);
          });
        }
        this.deptDatas = this.MINgetTreeData(res, null);
      });
    },

    // 加载树节点
    loadNodes(node) {
      this.loading = true;
      let typeUrlFn = "";
      const tagId = this.itm ? this.itm.tagId : this.tagId;
      switch (this.requestUrlType) {
        // 基准组织
        case "isBaseOrgTree":
          typeUrlFn = getBaseOrgTreeList({
            pid: node.id || null,
            keyword: "",
            sourceAppId: this.sourceAppType
          });
          break;

        case "isBusinessOrgTree":
          // 业务组织树
          typeUrlFn = getBusinessOrgTreeList({
            pid: node.id || null,
            keyword: "",
            tagId: tagId
          });
          break;
      }

      // 请求接口，获取数据
      typeUrlFn.then(res => {
        this.loading = false;
        if (!res || res.length === 0) {
          this.getChildrens(this.deptDatas, node.id, res);
          return;
        }

        // 解决节点展示空白的问题
        res.forEach(val => {
          val.children = [{}];
          this.defaultExpandedKeys.forEach((value, index) => {
            if (value === val.id) {
              this.defaultExpandedKeys.splice(index, 1);
            }
          });
        });

        if (node.id === "") {
          res.forEach(val => {
            this.deptDatas.push(val);
          });
          this.loadNodes(res[0]);
          this.defaultExpandedKeys.push(res[0].id);
          // 如果是树列表，就默认加载第一个节点数据
          if (this.treeType === "tree") {
            this.handleNodeClick(res[0]);
          }
        } else {
          this.getChildrens(this.deptDatas, node.id, res);
        }
      });
    },

    // 通过父级ID 拼接组织树
    getChildrens(nodes, pid, arr) {
      if (this.isChecked) {
        this.$nextTick(() => {
          this.$refs.deptTree.setCheckedNodes(this.defaultCheckedKeys);
        });
      }

      nodes.forEach(val => {
        if (val.id === pid) {
          val.children = arr;
        } else if (val.children && val.children.length > 0) {
          this.getChildrens(val.children, pid, arr);
        }
      });
    },

    // 拼接组织树
    MINgetTreeData(data, id) {
      if (!Array.isArray(data)) {
        return [];
      }
      let newData = [];
      newData = data.filter(v => v.pid === id);
      data = data.filter(v => v.pid);
      this.MINgetChildren(newData, data);
      return newData;
    },

    MINgetChildren(parentData, data) {
      if (!data || data.length === 0) return;
      parentData.forEach(v => {
        const newData = data.filter(v1 => {
          return v1.pid === v.id;
        });
        if (newData && newData.length > 0) {
          v.children = newData;
          this.defaultExpandedKeys.push(v.id);
          this.MINgetChildren(newData, data);
        } else {
          v.children = [{}];
        }
      });
    },

    // 设置默认展开
    setDefaultExpandedKeys() {
      if (this.deptDatas.length > 0) {
        this.deptDatas.forEach(val => {
          this.defaultExpandedKeys.push(val.id);
        });
      }
    }
  }
};
